<template>
  <ScreenModal
    :title="title"
    v-model="isVisible"
  >
    <div class="spring_box">
      <div class="spring_top spring_content">
        <div class="flex spring_bottom_box">
          <div class="spring_top_title">
            <span class="active_title">基本情况</span>
            <span class="active_tab"></span>
          </div>
        </div>
        <div class="spring_top_centent flex flex-wrap">
          <div
            :key="index"
            class="spring_top-item"
            v-for="(item, index) in infoList"
          >
            <div class="title">{{ item.name }}：</div>
            <div class="num">{{ item.value }}</div>
            <div
              class="sub-unit"
              v-if="item.outNum != 0"
            >
              <div><i class="el-icon-top"></i></div>
              <a>{{ item.outNum + item.unit }}</a>
            </div>
          </div>
        </div>
      </div>
      <div class="spring_bottom spring_content">
        <div class="flex spring_bottom_box spring_bottom_time">
          <div class="flex">
            <div
              :key="'a' + index"
              @click="changing(item.id)"
              class="spring_top_title"
              v-for="(item, index) in optionList"
            >
              <span
                :class="
                  optionId == item.id ? 'active_title' : 'active active_title'
                "
              >{{ item.title }}</span>
              <span
                class="active_tab"
                v-if="optionId == item.id"
              ></span>
            </div>
          </div>
          <div
            class="spring_bottom_right"
            v-if="optionId == 1"
          >
         
            <p
              :class="
                springBottomRight == 1
                  ? 'bottom_right_active1'
                  : 'bottom_right_active2'
              "
              @click="bottomRight(1)"
            >近24小时</p>
            <p
              :class="
                springBottomRight == 2  
                  ? 'bottom_right_active1'
                  : 'bottom_right_active2'
              "
              @click="bottomRight(2)"
            >近一周</p>

            <p>
            <el-date-picker
            :class="
                springBottomRight == 3  
                  ? 'bottom_right_active1'
                  : 'bottom_right_active2'
              "
              v-model="dateTime"
              type="datetimerange"
              placeholder="选择日期"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              format="yyyy-MM-dd hh:mm:ss"
              value-format="yyyy-MM-dd hh:mm:ss"
              @change="changeTime(3)"
            >
            </el-date-picker>
          </p>
          </div>
        </div>
        <div class="spring_text">
          <div
            class="echarts"
            id="echartsInfo"
            ref="echartsInfo"
            style="height: 200px"
            v-show="optionId == 1"
          ></div>
          <div
            class="yujing"
            v-show="optionId == 2"
          >
            <div
              :key="'b' + index"
              class="yujing_box"
              v-for="(item, index) in maxnums"
            >
              <!-- {{ item.title }} -->
              <p
                class="yujing_box_p"
                v-html="item.title"
              ></p>
            </div>
          </div>
        </div>
      </div>
    </div>
  </ScreenModal>
</template>

<script>
import { YangChenJianCeDB } from '@/api'

export default {
  name: 'popup',

  data() {
    return {
      dateTime:"",
      isVisible: false,
      optionList: [
        {
          title: '指标趋势',
          id: 1
        },
        {
          title: '监控预警',
          id: 2
        }
      ],
      optionId: 1,
      springBottomRight: 1,
      infoList: [],

      tabActive: 1, // 1, 2
      dateActive: 24, // 24, 7
      dateList: [
        { title: '近24小时', value: 24 },
        { title: '近一周', value: 7 },
      ],
      // 监测预警
      maxnums: [],
      point: [],
      code: '',
      title: ''
    }
  },
  computed: {
    isCharts() {
      return this.tabActive == 1
    }
  },
  mounted() {
    // this.getList();
    // todo 修改到getList 中
    // this.initChat();
    // this.getPointIndexNum();
    // this.getPointStandardTrend();
  },
  methods: {
    open(e) {
      this.optionId = 1
      this.code = e.number
      this.title = e.title
      this.isVisible = true
      this.getList()
      this.getPointIndexNum()
      this.getPointStandardTrend()
    },
    changing(id) {
      this.optionId = id
    },
    bottomRight(e) {
      this.dateTime = ''
      this.springBottomRight = e
      this.point = []
      this.getPointStandardTrend()
    },
    // //获取时间事件
    changeTime(e){
      this.springBottomRight = e
      this.point = []
      this.getPointStandardTrend()
    },
    // 基本情况数据
    getPointIndexNum() {
      const data = {
        code: this.code
      }
      YangChenJianCeDB.getPointIndexNum(data).then(res => {
        this.infoList = res.data
      })
    },

    // 监测预警数据
    getList() {
      const data = {
        code: this.code
      }
      YangChenJianCeDB.getPointMonitoring(data).then(res => {
        this.maxnums = res.data
      })
    },

    // 指标趋势
    getPointStandardTrend(type) {
      const data = {
        code: this.code,
        beginTime:this.springBottomRight == 3 ? this.dateTime[0] : '',
        endTime:this.springBottomRight == 3 ? this.dateTime[1] : '',
        type: this.springBottomRight
      }
      YangChenJianCeDB.getPointStandardTrend(data).then(res => {
        let arr = ['PM2.5', 'PM10', '总悬浮颗粒物', '噪声']
        res.data.forEach((item, index) => {
          this.point.push({
            title: arr[index],
            name: [],
            num: [],
            time: []
          })
          this.point[index].name = []
          this.point[index].num = []
          this.point[index].time = []
          item.forEach(items => {
            this.point[index].name.push(items.name)
            this.point[index].num.push(Number(items.value))
            this.point[index].time.push(this.getYMD(items.dataTime))
          })
        })
        if (type == 'tabChange') {
          var chart = echarts.getInstanceByDom(this.$refs.echartsInfo)
          var option = chart.getOption()
          option.series[0].data = this.point[0].num
          option.series[1].data = this.point[1].num
          option.series[2].data = this.point[2].num
          option.series[3].data = this.point[3].num
          chart.setOption(option)
        } else {
          this.echartsInit()
        }
      })
    },
    chartChange() {
      this.tabActive = this.tabActive == 1 ? 2 : 1
    },
    dateChange(value) {
      this.dateActive = value
      this.getList()
    },

    echartsInit() {
      const colorList = ['#9E87FF', '#73DDFF', '#fe9a8b', '#F56948', '#9E87FF']
      var myChart = echarts.init(this.$refs.echartsInfo)
      let option = {
        legend: {
          icon: 'circle',
          top: '0%',
          right: 0,
          align: 'left',
          itemWidth: 6,
          itemGap: 20,
          textStyle: {
            color: '#CFD2D6'
          }
        },
        tooltip: {
          trigger: 'axis',
          extraCssText:'z-index:999999999',
          confine: true,
          axisPointer: {
            label: {
              show: true,
              backgroundColor: '#fff',
              color: '#556677',
              borderColor: 'rgba(0,0,0,0)',
              shadowColor: 'rgba(0,0,0,0)',
              shadowOffsetY: 0
            },
            lineStyle: {
              width: 0
            }
          },
          backgroundColor: '#fff',
          textStyle: {
            color: '#5c6c7c'
          },
          padding: [10, 10],
          extraCssText: 'box-shadow: 1px 0 2px 0 rgba(163,163,163,0.5)',
          formatter: params => {
            let str = `${params[0].name}<br/>`
            params.forEach((v, i) => {
              str += `${params[i].seriesName}: ${params[i].value}${i === 3 ? 'dB' : 'μg/m³'}<br/>`
            })
            return str
          }
        },
        grid: {
          left: '0', // 固定左边刻度宽度
          right: '5%',
          bottom: '5%',
          containLabel: true
        },
        xAxis: [
          {
            show: true,
            type: 'category',
            data: this.point[1].time,
            boundaryGap: true,
            axisLabel: {
              textStyle: {
                color: '#9EADC9'
              }
            },
            axisLine: {
              lineStyle: {
                color: '#9EADC9'
              }
            }
          }
        ],

        yAxis: [
          {
            type: 'value',
            axisTick: {
              show: false
            },
            axisLine: {
              show: true,
              lineStyle: {
                color: '#9EADC9'
              }
            },
            axisLabel: {
              textStyle: {
                color: '#9EADC9'
              }
            },
            // 分割线
            splitLine: {
              show: false,
              lineStyle: {
                type: 'dashed',
                color: '#E9E9E9'
              }
            }
          }
        ],
        series: [
          {
            name: this.point[0].title,
            type: 'line',
            data: this.point[0].num,
            symbolSize: 1,
            symbol: 'circle',
            smooth: true,
            yAxisIndex: 0,
            showSymbol: false,
            lineStyle: {
              width: 4,
              color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [
                {
                  offset: 0,
                  color: '#9effff'
                }
              ])
            },
            itemStyle: {
              normal: {
                color: '#9effff',
                borderColor: colorList[1]
              }
            }
          },
          {
            name: this.point[1].title,
            type: 'line',
            data: this.point[1].num,
            symbolSize: 1,
            symbol: 'circle',
            smooth: true,
            yAxisIndex: 0,
            showSymbol: false,
            lineStyle: {
              width: 4,
              color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [
                {
                  offset: 0,
                  color: '#8920FF'
                }
              ])
            },
            itemStyle: {
              normal: {
                color: '#8920FF',
                borderColor: colorList[0]
              }
            }
          },
          {
            name: this.point[2].title,
            type: 'line',
            data: this.point[2].num,
            symbolSize: 1,
            symbol: 'circle',
            smooth: true,
            yAxisIndex: 0,
            showSymbol: false,
            lineStyle: {
              width: 4,
              color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [
                {
                  offset: 0,
                  color: '#FF8A00'
                }
              ])
            },
            itemStyle: {
              normal: {
                color: '#FF8A00',
                borderColor: colorList[0]
              }
            }
          },
          {
            name: this.point[3].title,
            type: 'line',
            data: this.point[3].num,
            symbolSize: 1,
            symbol: 'circle',
            smooth: true,
            yAxisIndex: 0,
            showSymbol: false,
            lineStyle: {
              width: 4,
              color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [
                {
                  offset: 0,
                  color: '#FE005A'
                }
              ])
            },
            itemStyle: {
              normal: {
                color: '#FE005A',
                borderColor: colorList[0]
              }
            }
          }
        ]
      }

      myChart.setOption(option, true)
    },

    onClose() {
      this.$emit('close')
      this.visibility = false
    },

    getYMD(data) {
      let myDate = new Date(data)
      let myMonth = myDate.getMonth() + 1 //获取当前月份(0-11,0代表1月)
      let myToday = myDate.getDate() //获取当前日(1-31)
      myMonth = myMonth > 9 ? myMonth : '0' + myMonth
      myToday = myToday > 9 ? myToday : '0' + myToday
      var hour = myDate.getHours() // 时
      var minutes = myDate.getMinutes() // 分
      if (hour >= 0 && hour <= 9) {
        hour = '0' + hour
      }
      if (minutes >= 0 && minutes <= 9) {
        minutes = '0' + minutes
      }
      let nowDate = ''
      if (this.springBottomRight != 2) {
        nowDate = myMonth + '/' + myToday + '\n' + hour + ':' + minutes
      } else {
        nowDate = myMonth + '/' + myToday
      }
      return nowDate
    }
  }
}
</script>

<style lang="less" scoped>
.spring_box {
  width: 100%;
  height: 100%;
  padding-top: 40px;
  display: flex;
  flex-direction: column;
  .spring_content {
    background: linear-gradient(0deg, rgba(18, 81, 210, 0.3), rgba(33, 158, 255, 0.1));
    border: 1px solid #219eff;
    border-radius: 10px;
    .spring_bottom_box {
      border-bottom: 1px solid rgba(185, 224, 255, 0.2);
    }
    .spring_top_title {
      cursor: pointer;
      display: flex;
      flex-direction: column;
      align-items: center;
      margin-right: 30px;
      span {
        display: inline-block;
      }
      .active_title {
        font-size: 16px;
        font-family: Source Han Sans SC;
        font-weight: 500;
        color: #ffffff;
      }
      .active_tab {
        width: 28px;
        height: 4px;
        margin-top: 12px;
        background: #17a7eb;
      }
      .active {
        font-size: 14px;
        font-family: Source Han Sans SC;
        font-weight: 400;
        color: #75aada;
      }
    }
  }
  .spring_top {
    padding: 20px 30px;
    margin-bottom: 20px;
    // display: flex;
    .spring_top_centent {
      width: 100%;
      .spring_top-item {
        width: 33%;
        margin-top: 20px;
        display: flex;
        // justify-content: start;
        align-items: center;
        .title {
          font-size: 14px;
          font-family: Source Han Sans SC;
          font-weight: 400;
          color: #ffffff;
          line-height: 24px;
        }
        .num {
          font-size: 14px;
          font-family: Source Han Sans SC;
          font-weight: 400;
          color: #9ed5ff;
          line-height: 24px;
        }
        .sub-unit {
          display: flex;
          color: red;
          div {
            margin: 0 5px 0 10px;
            i{
              font-weight: bold;
            }
          }
          a {
            text-decoration: underline;
          }
        }
      }
    }
  }
  .spring_bottom {
    padding: 20px 30px;
    .spring_bottom_time {
      display: flex;
      justify-content: space-between;
      margin-bottom: 24px;
    }
    .spring_bottom_right {
      display: flex;
      p {
        // &:first-child{
        //   width: 130px;
        // }
        height: 20px;
        background: rgba(31, 73, 135, 0.3);
        opacity: 0.6;
        border-radius: 10px;
        margin-left: 10px;
        font-size: 12px;
        font-family: Source Han Sans CN;
        font-weight: 400;
        color: #ffffff;
        padding: 0 10px;
        cursor: pointer;
      }
     
      .bottom_right_active1 {
        border: 1px solid #3cffff;
      }
      .bottom_right_active2 {
        border: 1px solid #2368ff;
      }
    }
    .spring_text {
      max-height: 260px;
      overflow: auto;
      .yujing {
        .yujing_box {
          width: 100%;
          background: url('../assets/images/content/moduleItembj.png') no-repeat;
          background-size: 100% 100%;
          font-size: 14px;
          font-family: Noto Sans S Chinese;
          font-weight: 400;
          color: #ffffff;
          line-height: 22px;
          padding: 20px;
          margin-bottom: 10px;
          p {
            span {
              color: red;
              span {
                color: #fff;
              }
            }
          }
        }
      }
    }
  }
}
/deep/ .el-input__inner{
  height: 20px;
  background: rgba(31, 73, 135, 0.3);
  border-radius: 20px;
  .el-range-input{
    background: transparent;
    color: #fff;
  }
 }
/deep/ .el-input__icon {
  line-height: 15px;
 }

</style>
<style lang="less">
.yujing_box_p {
  span {
    color: red;
    span {
      color: #fff;
    }
  }
}

.el-picker-panel{
  background: rgba(31, 73, 135, 0.6);
  color: #fff;
  .el-input__inner{
    background: rgba(31, 73, 135, 0.6);
  }
  .el-picker-panel__footer{
    background: rgba(31, 73, 135, 0.6);
      .el-button{
        background-color:rgba(31, 73, 135, 0.6) ;
      }
  }
 } 

.btn-close{
  cursor: pointer;
}


</style>
